define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            Table.api.init({
                extend: {
                    index_url: 'scanwork/schedule/index' + location.search,
                    add_url: 'scanwork/schedule/add',
                    edit_url: 'scanwork/schedule/edit',
                    del_url: 'scanwork/schedule/del',
                    table: 'scanwork_schedule',
                }
            });

            var table = $("#table");

            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: 'ID', width: 50},
                        {field: 'schedule_no', title: '排程编号', width: 120},
                        {field: 'schedule_name', title: '排程名称', width: 150},
                        {field: 'schedule_type', title: '排程类型', width: 100, formatter: function(v) {
                            var typeMap = {1: '主排程', 2: '子排程', 3: '紧急排程'};
                            return typeMap[v] || '未知';
                        }},
                        {field: 'start_date', title: '开始日期', width: 100, formatter: Table.api.formatter.date},
                        {field: 'end_date', title: '结束日期', width: 100, formatter: Table.api.formatter.date},
                        {field: 'algorithm', title: '排程算法', width: 120, formatter: function(v) {
                            var algorithmMap = {
                                'priority': '优先级算法',
                                'shortest_job': '最短作业优先',
                                'earliest_deadline': '最早截止时间',
                                'critical_path': '关键路径法'
                            };
                            return algorithmMap[v] || v;
                        }},
                        {field: 'status', title: '状态', width: 80, searchList: {0:'草稿',1:'已发布',2:'执行中',3:'已完成'}, formatter: function(v) {
                            var statusMap = {0: '草稿', 1: '已发布', 2: '执行中', 3: '已完成'};
                            var colorMap = {0: 'warning', 1: 'success', 2: 'info', 3: 'primary'};
                            return '<span class="label label-' + (colorMap[v] || 'default') + '">' + (statusMap[v] || '未知') + '</span>';
                        }},
                        {field: 'creator_name', title: '创建人', width: 80},
                        {field: 'createtime', title: '创建时间', width: 120, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, 
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'gantt',
                                    text: '甘特图',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-calendar',
                                    url: 'scanwork/schedule/gantt',
                                    extend: 'data-area=\'["90%","90%"]\'',
                                }
                            ]
                        }
                    ]
                ]
            });

            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        gantt: function () {
            // 甘特图初始化代码
            console.log('甘特图页面');
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});

